package com.woan.api.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;

import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysAdmininfoMobile;
import com.woan.api.model.SysMessage;
import com.woan.api.model.SysUpgrade;
import com.woan.api.model.SysVideo;
import com.woan.api.model.SysVideoBasic;
import com.woan.api.services.MessageClientServices;
import com.woan.api.services.PhotoClientServices;
import com.woan.api.services.UpgradeClientServices;
import com.woan.api.services.UserClientServices;
import com.woan.api.services.VideoClientServices;
import com.woan.api.util.ConfigUtil;
import com.woan.api.util.DateJsonValueProcessor;
import com.woan.api.util.FuncDate;
import com.woan.api.util.Md5Util;
import com.woan.api.util.Randoms;
import com.woan.api.vo.Json;
import com.woan.api.vo.VClientvideoList;
import com.woan.api.vo.VloginList;
import com.woan.api.vo.VmessageList;
import com.woan.api.vo.VphotoList;
import com.woan.api.vo.VpositionList;



public class XzbApi {
	private HttpServletRequest request;
	private HttpServletResponse response;
	//返回json内容
	public void reuturnjson(String json) throws IOException{
		
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setHeader("Cache-control", "no-store");// HTTP 1.0
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setCharacterEncoding("utf-8");// 设置编码
		PrintWriter out;
		out = response.getWriter();
		out.println(json);
		out.flush();
		out.close();	
	}
	//系统组注册
	public void groupregist() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间		
		request = ServletActionContext.getRequest();// 初始request
		JSONObject json=new JSONObject();
		String groupname = request.getParameter("groupname");
		String groupdesc = request.getParameter("groupdesc");
		String grouppwd = request.getParameter("grouppwd");
		String usercount = request.getParameter("usercount");
		json = userClientServices.groupRegist(groupname, groupdesc,grouppwd,usercount);
		reuturnjson(json.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("组注册程序运行时间： "+(endTime-startTime)+"ms");
	}
	//系统用户注册
	public void userregist() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间		
		request = ServletActionContext.getRequest();// 初始request
		JSONObject json=new JSONObject();
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String nickname = request.getParameter("nickname");
		String userpwd = request.getParameter("userpwd");
		String days = request.getParameter("days");
		json = userClientServices.userRegist(groupname, username,nickname,userpwd,days);
		reuturnjson(json.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("用户注册程序运行时间： "+(endTime-startTime)+"ms");
	}	
	//系统登录判断
	public void logininfo() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间	
		request = ServletActionContext.getRequest();// 初始request
		Json json = new Json();
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		System.out.println(devicenum);
		String devicetoken = request.getParameter("deviceToken");
		String devicetype =  request.getParameter("deviceType");
		System.out.println(devicetype);
		String devicesys =  request.getParameter("deviceSys");
		String pushtype =  request.getParameter("pushtype");
		String servicecode =  request.getParameter("scode");
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		if(json.getResults().equals("0"))
		{
			String auth="";String usertype="";
			if(json.getMobile()==null)
			{
				auth = "0";
				usertype ="0";
			}
			else
			{
				auth = json.getMobile().getIsauth();
				usertype = json.getMobile().getUserType();
			}
			userClientServices.saveMobileInfo(json.getAdmin().getUid(),auth,usertype,devicenum,devicetoken,devicesys,devicetype,pushtype,servicecode);
		}
		JSONObject rsjson=new JSONObject();
		rsjson.accumulate("results", Integer.parseInt(json.getResults()));
		if(json.getItems()==null)
			rsjson.accumulate("items", "");
		else
			rsjson.accumulate("items", json.getItems());
		rsjson.accumulate("errMsg", json.getErrMsg());
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("登陆程序运行时间： "+(endTime-startTime)+"ms");
	}
	//ios退出系统
	public void ioslogout() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间	
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		JSONObject rsjson=new JSONObject();
		rsjson=userClientServices.ioslogout(groupname, username,userpwd,devicenum);
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("ios退出系统运行时间： "+(endTime-startTime)+"ms");
	}
	//更新用户地理位置相关信息
	public void updateuser() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间	
		request = ServletActionContext.getRequest();// 初始request
		Json json = new Json();
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String countrycode = request.getParameter("countrycode");
		String countryname = request.getParameter("countryname");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String counties = request.getParameter("counties");
		String devicenum = request.getParameter("deviceNum");
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			rsjson=userClientServices.updateUser(json.getAdmin().getUid(),longitude,latitude,countrycode,countryname,province,city,counties);
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("更新地理位置运行时间： "+(endTime-startTime)+"ms");
	}
	//编辑或插入视频-标题、描述、位置信息
	public void updatevideo() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String vid =  request.getParameter("vid");
		String title =  request.getParameter("title");
		String address =  request.getParameter("address");
		String longitude =  request.getParameter("longitude");
		String latitude =  request.getParameter("latitude");
		String detail =  request.getParameter("detail");
		String countrycode = request.getParameter("countrycode");
		String countryname = request.getParameter("countryname");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String counties = request.getParameter("counties");
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		String flag = "1";
		if(json.getResults().equals("0"))
		{
			if(vid != null && !"".equals(vid))
			{
				flag = videoClientServices.editVideos(json.getAdmin().getSysRole().getRid(),json.getAdmin().getUid(),json.getAdmin().getUserName(),vid, title,address,longitude,latitude,detail,countrycode,countryname,province,city,counties);
				if(flag.equals("0"))
				{
					
					rsjson.accumulate("results", 0);
					
					rsjson.accumulate("errMsg", "更新成功！");
				}
				else
				{
					rsjson.accumulate("results", 1);
					
					rsjson.accumulate("errMsg", "更新失败！");
				}
			}
			else
			{
				rsjson.accumulate("results", 3);
				
				rsjson.accumulate("errMsg", "视频id不能为空");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("编辑视频程序运行时间： "+(endTime-startTime)+"ms");
	}
	//直播列表
	public void getlivevideos() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String pageid = request.getParameter("pageid");
		String pagesize = request.getParameter("pagesize");
		if(pageid == null || "".equals(pageid)){
			pageid = "1";
		}
		if(pagesize == null || "".equals(pagesize)){
			pagesize = "10";
		}
		Integer beginIndex = (Integer.parseInt(pageid)-1)  * Integer.parseInt(pagesize);
		Integer endIndex = Integer.parseInt(pagesize);
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			List<VClientvideoList> video = null;
			video = videoClientServices.listLiveVideos(json.getAdmin().getSysRole().getRid(),json.getMobile().getPushType(),beginIndex, endIndex);
			if(video.size()>0){
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "当前没有直播视频！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("直播列表程序运行时间： "+(endTime-startTime)+"ms");
	}
	//历史列表
	public void gethistoryvideos() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String pageid = request.getParameter("pageid");
		String pagesize = request.getParameter("pagesize");
		if(pageid == null || "".equals(pageid)){
			pageid = "1";
		}
		if(pagesize == null || "".equals(pagesize)){
			pagesize = "10";
		}
		Integer beginIndex = (Integer.parseInt(pageid)-1)  * Integer.parseInt(pagesize);
		Integer endIndex = Integer.parseInt(pagesize);
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			List<VClientvideoList> video = null;
			video = videoClientServices.listHistoryVideos(json.getAdmin().getSysRole().getRid(),json.getMobile().getPushType(), beginIndex, endIndex);
			if(video.size()>0)
			{
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到结果！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("历史列表程序运行时间： "+(endTime-startTime)+"ms");
	}
	//我的视频
	public void getmyvideos() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String pageid = request.getParameter("pageid");
		String pagesize = request.getParameter("pagesize");
		if(pageid == null || "".equals(pageid)){
			pageid = "1";
		}
		if(pagesize == null || "".equals(pagesize)){
			pagesize = "10";
		}
		Integer beginIndex = (Integer.parseInt(pageid)-1)  * Integer.parseInt(pagesize);
		Integer endIndex = Integer.parseInt(pagesize);
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			List<VClientvideoList> video = null;
			video = videoClientServices.listMyVideos(json.getAdmin().getUid(),json.getMobile().getPushType(), beginIndex, endIndex);
			if(video.size()>0)
			{
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到结果！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("我的视频程序运行时间： "+(endTime-startTime)+"ms");
	}
	//获取历史视频总数
	public void gethistoryvideoscount() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		//获取总数
		Integer total = 0;
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			total = videoClientServices.listHistoryVideosCount(json.getAdmin().getSysRole().getRid(),json.getMobile().getPushType());
			if(total>0)
			{
				
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", total);
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "没有历史视频！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("获取历史视频总数程序运行时间： "+(endTime-startTime)+"ms");
	}
	//获取我的视频总数
	public void getmyvideoscount() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		//获取总数
		Integer total = 0;
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			total = videoClientServices.listMyVideosCount(json.getAdmin().getUid(),json.getMobile().getPushType());
			if(total>0)
			{
				
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", total);
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "没有视频！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("获取我的视频总数程序运行时间： "+(endTime-startTime)+"ms");
	}
	//获取单个视频信息
	public void getonevideo() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String vid = request.getParameter("vid");
		//获取总数
		Integer total = 0;
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			SysVideo video = null;
			video = videoClientServices.listOneVideoinfo(vid,json.getMobile().getPushType());
			if(video!=null)
			{
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "没有视频！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("获取单个视频信息程序运行时间： "+(endTime-startTime)+"ms");
	}
	//发送文字消息
	public void sendmessage() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String text = request.getParameter("text");
		FuncDate fd = new FuncDate();
		SysMessage sysmessage = new SysMessage();
		sysmessage.setMid(fd.FindCurSysTime()+Randoms.getRandomString(3));
		sysmessage.setMsgText(text);
		sysmessage.setSendtime(fd.getSysTime());
		sysmessage.setIsdel("1");
		sysmessage.setMsgType("1");
		sysmessage.setUserName(username);
		sysmessage.setVoicelength(0);
		sysmessage.setMsg_web(text);
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			rsjson = messageClientServices.sendMessage(groupname, username,json.getAdmin().getSysRole().getRid(),json.getAdmin().getUid(),sysmessage);
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("发送文字消息程序运行时间： "+(endTime-startTime)+"ms");
	}
	//组内消息列表
	public void getgroupmessage() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		Integer pagesize=10;
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String pageid = request.getParameter("pageid");
		if(pageid == null || "".equals(pageid)){
			pageid = "1";
		}
		Integer beginIndex = (Integer.parseInt(pageid)-1)  * pagesize;
		Integer endIndex = pagesize;
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			List<VmessageList> message = messageClientServices.listHistoryMessage(json.getAdmin().getSysRole().getRid(), beginIndex, endIndex);
			if(message.size()>0)
			{
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(message, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到数据！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString()); 
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("查询组内消息程序运行时间： "+(endTime-startTime)+"ms");
	}
	//组内照片列表
	public void getgroupphotos() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		Integer pagesize=10;
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String pageid = request.getParameter("pageid");
		if(pageid == null || "".equals(pageid)){
			pageid = "1";
		}
		Integer beginIndex = (Integer.parseInt(pageid)-1)  * pagesize;
		Integer endIndex = pagesize;
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		List<VphotoList> photo = null;
		if(json.getResults().equals("0"))
		{
			photo = photoClientServices.getgroupphotos(json.getAdmin().getSysRole().getRid(), beginIndex, endIndex);
			if(photo.size()>0)
			{
				for (VphotoList v_photo : photo) {
					v_photo.setSmurl(ConfigUtil.get("serverinfo")+v_photo.getSmurl());
					v_photo.setUrl(ConfigUtil.get("serverinfo")+v_photo.getUrl());
				}
				
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(photo, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到数据！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString()); 
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("照片列表程序运行时间： "+(endTime-startTime)+"ms");
	}
	//我的照片列表
	public void getmyphotoes() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		Integer pagesize=10;
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String pageid = request.getParameter("pageid");
		if(pageid == null || "".equals(pageid)){
			pageid = "1";
		}
		Integer beginIndex = (Integer.parseInt(pageid)-1)  * pagesize;
		Integer endIndex = pagesize;
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		List<VphotoList> photo = null;
		
		if(json.getResults().equals("0"))
		{
			photo = photoClientServices.getmyphotoes(json.getAdmin().getUid(), beginIndex, endIndex);
			if(photo!=null&&photo.size()>0){
				for (VphotoList v_photo : photo) {
					v_photo.setSmurl(ConfigUtil.get("serverinfo")+v_photo.getSmurl());
					v_photo.setUrl(ConfigUtil.get("serverinfo")+v_photo.getUrl());
				}
				
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(photo, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到数据！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString()); 
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("照片列表程序运行时间： "+(endTime-startTime)+"ms");
	}
	//编辑照片
	public void updatephoto() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String pid = request.getParameter("pid");
		String title = request.getParameter("title");
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			if(pid != null && !"".equals(pid))
			{
				rsjson = photoClientServices.editPhotos(pid, title);
				
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("errMsg", "没有此照片！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("编辑照片标题程序运行时间： "+(endTime-startTime)+"ms");
	}
	//查看本组人员位置信息
	public void getgroupuserposition() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String pageid =request.getParameter("pageid");
		if(pageid==null || pageid.equals(""))
			pageid="1";
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0"))
		{
			if(Integer.parseInt(pageid)<2)
			{
				List<VpositionList> position = videoClientServices.listVideoPostion(json.getAdmin().getSysRole().getRid(),json.getMobile().getPushType());
				if(position.size()>0)
				{
					JsonConfig jsonConfig = new JsonConfig();
					jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
					JSONArray jsonArray = JSONArray.fromObject(position, jsonConfig);
					rsjson.accumulate("results", 0);
					rsjson.accumulate("items", jsonArray.toString());
					rsjson.accumulate("errMsg", "查到结果！");
				}
				else
				{
					rsjson.accumulate("results", 0);
					rsjson.accumulate("items", "");
					rsjson.accumulate("errMsg", "未查到数据！");
				}
			}
			else
			{
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到数据！");
			}
		}
		else
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("位置信息程序运行时间： "+(endTime-startTime)+"ms");
	}
	//客户端申请鉴权
	public void getuserauth() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		Json json = new Json();
		json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		if(json.getResults().equals("0") || json.getResults().equals("2"))
		{
			SysAdmininfoMobile mobile = userClientServices.findMobileByUserid(json.getAdmin().getUid());
			mobile.setUserType("2");
			boolean flag = userClientServices.saveMobile(mobile);
			if(flag)
			{
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "申请成功！");
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "申请失败！");
			}
		}
		else 
		{
			rsjson.accumulate("results", 2);
			rsjson.accumulate("items", "");
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("客户端申请鉴权程序运行时间： "+(endTime-startTime)+"ms");
	}
	//一键获取登录信息
	public void getuserinfo() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String devicenum = request.getParameter("deviceNum");
		JSONObject rsjson=new JSONObject();
		List<VloginList> loginList =userClientServices.findAdminInfoByDeviceNum(devicenum);
		if(loginList.size()>0)
		{
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
			JSONArray jsonArray = JSONArray.fromObject(loginList, jsonConfig);
			rsjson.accumulate("results", 0);
			rsjson.accumulate("items", jsonArray.toString());
			rsjson.accumulate("errMsg", "查到结果！");
		}
		else
		{
			rsjson.accumulate("results", 0);
			rsjson.accumulate("items", "");
			rsjson.accumulate("errMsg", "未查到数据！");
		}
		reuturnjson(rsjson.toString());
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("一键获取信息程序运行时间： "+(endTime-startTime)+"ms");
	}
	
	//系统升级---地址固定不能修改
	public void upgrade() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String requrl =  request.getRequestURL().toString();
		System.out.println( requrl);
		System.out.println( "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		String[] urlsplit = requrl.split(":");
		String groupname = request.getParameter("fristname");
		String username = request.getParameter("userName");
		String userpwd = request.getParameter("passwd");
		String devicenum = request.getParameter("deviceNum");
		String os = request.getParameter("os");
		String vercode = request.getParameter("versionCode");
		System.out.println("手机端版本号是"+vercode);
		String vername =  request.getParameter("versionName");
		System.out.println("手机端版本名称:("+vername+")");
		//Json json = new Json();
		//json = userClientServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson=new JSONObject();
		//if(json.getResults().equals("0"))
		//{
			SysUpgrade sysupgrade=  upgradeClientServices.listNewVersion(os, vername);
			// 获取资源路径
			ResourceBundle rb_config = ResourceBundle.getBundle("config");
			String videorealpath = rb_config.getString("versionsPath");
			
			if (sysupgrade==null) {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未获取到版本信息");
				return;
			}
			
			//如果当前系统是ios
			if ("ios".equals(sysupgrade.getOs())) {
				videorealpath+="\\ios"+"\\"+sysupgrade.getUrl();
			}else {
				videorealpath=urlsplit[0]+":"+urlsplit[1]+videorealpath+"/"+sysupgrade.getUrl();
				System.out.println("服务器版本地址url："+videorealpath);
			}
			sysupgrade.setUrl(videorealpath);
			System.out.println("服务器上版本信息:"+"版本名称"+sysupgrade.getVersionname()+"&&&"+"版本号："+sysupgrade.getVersioncode()+"&&&"+"版本路径："+sysupgrade.getUrl());
			if(sysupgrade!=null && sysupgrade.getVersioncode()!=null)
			{
				String items = JSONObject.fromObject(sysupgrade).toString();
				System.out.println(items);
				String[] comcode = vercode.split("\\.");
				String[] curcode= sysupgrade.getVersioncode().split("\\.");
					if(Integer.parseInt(curcode[0]) > Integer.parseInt(comcode[0]))
					{
						rsjson.accumulate("results", 0);
						rsjson.accumulate("items", items);
						rsjson.accumulate("errMsg", "");
					}
					else if(Integer.parseInt(curcode[0]) == Integer.parseInt(comcode[0]))
					{
						if(Integer.parseInt(curcode[1]) > Integer.parseInt(comcode[1]))
						{
							rsjson.accumulate("results", 0);
							rsjson.accumulate("items", items);
							
							rsjson.accumulate("errMsg", "");
						}

						else
						{
							rsjson.accumulate("results", 1);
							rsjson.accumulate("items", "");
							rsjson.accumulate("errMsg", "未获取到版本信息");
						}
					}
					else
					{
						rsjson.accumulate("results", 1);
						rsjson.accumulate("items", "");
						rsjson.accumulate("errMsg", "未获取到版本信息");
					}	
			}
			else
			{
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未获取到版本信息");
			}
		//}
		//else
		//{
		//	rsjson.accumulate("results", 2);
		//	rsjson.accumulate("errMsg", "用户信息错误！");
		//}
		reuturnjson(rsjson.toString()); 
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("系统升级程序运行时间： "+(endTime-startTime)+"ms");
	}
	//以下是2013-12-27增加的双向视频语音使用的接口
	//将视频会传给拍摄人
	public void returnvideomsg() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		ResourceBundle rb_config = ResourceBundle.getBundle("config");
		String speaker_text=rb_config.getString("speaker_text");
		String random_seed=rb_config.getString("random_seed");
		String hash_key=rb_config.getString("hash_key");
		String fromuser = request.getParameter("fromuser");
		String videoid = request.getParameter("videoid");
		String newvideoid = request.getParameter("newvideoid");
		SysVideoBasic sysvideobasic = videoClientServices.getBasicVideoByVid(videoid);
		SysAdmininfo admin = userClientServices.findUserByUserid(sysvideobasic.getUid());
		SysAdmininfoMobile sysmobile = userClientServices.findMobileByUserid(sysvideobasic.getUid());
		String to_user_name_really=admin.getHigher()+"|"+admin.getUserName()+"|"+sysmobile.getDeviceNum();
		String msg =Md5Util.string2MD5("woan")+"|"+"camera"+"|play"+"|"+newvideoid;
		String urrStr = "";
		URL url = null;
		InputStream is = null;
		urrStr=speaker_text+"?dst_username="+to_user_name_really+"&from_username="+fromuser+"&message="+msg+"&stream_id="+videoid+"&hash_key="+hash_key+"&random_seed="+random_seed;
		System.out.println(urrStr);
		try {
			url = new URL(urrStr);
			is = url.openStream();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("通知视频回传运行时间： "+(endTime-startTime)+"ms");
	}
	public void begintalk() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		ResourceBundle rb_config = ResourceBundle.getBundle("config");
		String speaker_text=rb_config.getString("speaker_text");
		String random_seed=rb_config.getString("random_seed");
		String hash_key=rb_config.getString("hash_key");
		String fromuser = request.getParameter("fromuser");
		String fromvideoid = request.getParameter("fromvideoid");
		String tovideoid = request.getParameter("tovideoid");
		SysVideoBasic sysvideobasic = videoClientServices.getBasicVideoByVid(tovideoid);
		SysAdmininfo admin = userClientServices.findUserByUserid(sysvideobasic.getUid());
		SysAdmininfoMobile sysmobile = userClientServices.findMobileByUserid(sysvideobasic.getUid());
		String to_user_name_really=admin.getHigher()+"|"+admin.getUserName()+"|"+sysmobile.getDeviceNum();
		String msg =Md5Util.string2MD5("woan")+"|"+"audio"+"|play"+"|"+fromvideoid;
		
		String urrStr = "";
		URL url = null;
		InputStream is = null;
		urrStr=speaker_text+"?dst_username="+to_user_name_really+"&from_username="+fromuser+"&message="+msg+"&stream_id="+tovideoid+"&hash_key="+hash_key+"&random_seed="+random_seed;
		System.out.println(urrStr);
		try {
			url = new URL(urrStr);
			is = url.openStream();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("通知发起语音消息运行时间： "+(endTime-startTime)+"ms");
	}
	public void endtalk() throws IOException{
		long startTime=System.currentTimeMillis();   //获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		ResourceBundle rb_config = ResourceBundle.getBundle("config");
		String speaker_text=rb_config.getString("speaker_text");
		String random_seed=rb_config.getString("random_seed");
		String hash_key=rb_config.getString("hash_key");
		String fromuser = request.getParameter("fromuser");
		String fromvideoid = request.getParameter("fromvideoid");
		String tovideoid = request.getParameter("tovideoid");
		SysVideoBasic sysvideobasic = videoClientServices.getBasicVideoByVid(tovideoid);
		SysAdmininfo admin = userClientServices.findUserByUserid(sysvideobasic.getUid());
		SysAdmininfoMobile sysmobile = userClientServices.findMobileByUserid(sysvideobasic.getUid());
		String to_user_name_really=admin.getHigher()+"|"+admin.getUserName()+"|"+sysmobile.getDeviceNum();
		String msg =Md5Util.string2MD5("woan")+"|"+"audio"+"|stop"+"|"+fromvideoid;
		
		String urrStr = "";
		URL url = null;
		InputStream is = null;
		urrStr=speaker_text+"?dst_username="+to_user_name_really+"&from_username="+fromuser+"&message="+msg+"&stream_id="+tovideoid+"&hash_key="+hash_key+"&random_seed="+random_seed;
		System.out.println(urrStr);
		try {
			url = new URL(urrStr);
			is = url.openStream();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		long endTime=System.currentTimeMillis(); //获取结束时间
		System.out.println("通知停止语音消息运行时间： "+(endTime-startTime)+"ms");
	}
	private UserClientServices userClientServices;
	private VideoClientServices videoClientServices;
	private PhotoClientServices photoClientServices;
	private MessageClientServices messageClientServices;
	private UpgradeClientServices upgradeClientServices;
	
	public UpgradeClientServices getUpgradeClientServices() {
		return upgradeClientServices;
	}
	public void setUpgradeClientServices(UpgradeClientServices upgradeClientServices) {
		this.upgradeClientServices = upgradeClientServices;
	}
	public UserClientServices getUserClientServices() {
		return userClientServices;
	}
	public void setUserClientServices(UserClientServices userClientServices) {
		this.userClientServices = userClientServices;
	}
	public VideoClientServices getVideoClientServices() {
		return videoClientServices;
	}
	public void setVideoClientServices(VideoClientServices videoClientServices) {
		this.videoClientServices = videoClientServices;
	}
	public PhotoClientServices getPhotoClientServices() {
		return photoClientServices;
	}
	public void setPhotoClientServices(PhotoClientServices photoClientServices) {
		this.photoClientServices = photoClientServices;
	}
	public MessageClientServices getMessageClientServices() {
		return messageClientServices;
	}
	public void setMessageClientServices(MessageClientServices messageClientServices) {
		this.messageClientServices = messageClientServices;
	}

	
}
